package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.Device;
import com.iot.ic.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by xiuge on 2017/7/10.
 */
@Api(description = "设备信息相关接口")
@RestController
public class DeviceController extends BaseController{

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value="获取设备列表", notes="")
    @GetMapping(value = "/devices")
    public Response getDeviceList(){
        Response response=new Response();
        List<Device> devices = deviceService.getDeviceList();
        response.setResult(devices);
        return response;
    }

    @ApiOperation(value="添加设备", notes="")
    @PostMapping("/users/{userId}/devices")
    public Response addDevice(@PathVariable("userId")int userId,@RequestBody @Valid Device device){
        Response response=new Response();
        int result=deviceService.addDevice(device);
        if (result==1)
            response.setInfos(ReturnCode.No_AddDeviceOK);
        else
            response.setInfos(ReturnCode.Err_AddDeviceFailor);
        return  response;
    }

    @ApiOperation(value="删除指定设备", notes="")
    @DeleteMapping("/users/{userId}/devices/{deviceId}")
    public Response delDevice(@PathVariable("userId")int userId,@PathVariable("deviceId") int deviceId){
        Response response=new Response();
        int result=deviceService.deleteDevice(deviceId);
        if (result==1)
            response.setInfos(ReturnCode.No_DeleteDeviceOK);
        else
            response.setInfos(ReturnCode.Err_DeleteDeviceFailor);
        return  response;
    }
    
    @ApiOperation(value="获得设备详情", notes="")
    @GetMapping(value = "/users/{userId}/devices/{deviceId}")
    public Response getDevice(@PathVariable("userId")int userId,@PathVariable("deviceId") int deviceId){
        Response response=new Response();
        Device device = deviceService.getDeviceById(deviceId);
        response.setResult(device);
        return  response;
    }
    
    
    @ApiOperation(value="绑定车队与设备信息", notes="")
    @PostMapping("/users/{userId}/devices/{deviceId}/team/{teamId}")
    public Response bindDeviceWithTeam(@PathVariable("userId")int userId,
    		@PathVariable("deviceId") int deviceId,
    		@PathVariable("teamId") int teamId){
        Response response = new Response();
        int result=deviceService.bindDeviceWithTeam(deviceId, teamId);
        if (result==1)
            response.setInfos(ReturnCode.No_Err);
        else
            response.setInfos(ReturnCode.Err_RequestFailed);
        return  response;
    }
    
    
    @ApiOperation(value="解除车队与设备绑定", notes="")
    @PostMapping("/users/{userId}/devices/{deviceId}/unBind")
    public Response unBindDeviceWithTeam(@PathVariable("userId")int userId,
    		@PathVariable("deviceId") int deviceId){
        Response response = new Response();
        int result = deviceService.unBindDeviceWithTeam(deviceId);
        if (result==1)
            response.setInfos(ReturnCode.No_Err);
        else
            response.setInfos(ReturnCode.Err_RequestFailed);
        return  response;
    }
    
    @ApiOperation(value="关闭设备", notes="")
    @PostMapping("/users/{userId}/devices/{deviceId}/close")
    public Response closeDevice(@PathVariable("userId")int userId,
    		@PathVariable("deviceId") int deviceId){
        Response response = new Response();
        int result = deviceService.closeDevice(deviceId);
        if (result==1)
            response.setInfos(ReturnCode.No_Err);
        else
            response.setInfos(ReturnCode.Err_RequestFailed);
        return  response;
    }


}
